package com.czk.gulimall.search.thread;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 任务的串行化
 *
 * @Author:ChenZhangKun
 * @Date: 2022/2/10 15:28
 */
public class CompletableFutureTest2 {
    private static ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture.supplyAsync(() -> {
            System.out.println(1);
            return 1;
        }, executor)
                // 不能获取上一步的结果，无返回值
                .thenRunAsync(() -> {
                    System.out.println("任务1启动了");
                }, executor);

        CompletableFuture.supplyAsync(() -> {
            System.out.println(1);
            return 1;
        }, executor)
                // 能获取上一步的结果,没有返回值
                .thenAcceptAsync(res -> {
                    System.out.println("任务1启动了");
                    // 打印上一次的结果
                    System.out.println(res);
                }, executor);


        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println(1);
            return 1;
        }, executor)
                // 能获取上一步的结果,没有返回值
                .thenApplyAsync(res -> {
                    System.out.println("任务1启动了");
                    // 打印上一次的结果
                    System.out.println(res);
                    // 对结果进行处理
                    return res * 2;
                }, executor);
        System.out.println(future1.get());

    }

}
